package yz.com.javautil.utilpackage;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
public class Tools {
    /**
     * 带逗号的字符串转换成SQL里的inStr（"a,b,c"->'a','b','c'）
     *
     * @param stringWithComma 用逗号分隔的字符串 "a,b,c"
     * @return inStr可以用在SQL的in语句内 'a','b','c'
     */
    public String getInStr(String stringWithComma) {
        String splitSymbolComma = ",";
        ArrayList<String> strList = CollectionUtil.toList(stringWithComma.split(splitSymbolComma));
        return strList.stream().collect(Collectors.joining("\',\'", "\'", "\'"));
    }

    /**
     * 未分页数据根据参数进行分页
     *
     * @param rows     未分页的数据记录
     * @param curPage  页码（从1开始）
     * @param pageSize 每页记录数据
     * @return 当前页码的记录数据
     */

    public static List<?> rowsToPageRows(List<?> rows, int curPage, int pageSize) {
        int totalSize = rows.size();
        int totalPage = ((Double) Math.ceil(totalSize * 1.0 / pageSize)).intValue();
        // 当前页大于总页数
        if (curPage > totalPage) {
            return null;
        }
        // 计算的 toIndex 大于总记录数则取总条数
        int toIndex = pageSize * curPage;
        if (toIndex > totalSize) {
            toIndex = totalSize;
        }
        return rows.subList(pageSize * (curPage - 1), toIndex);
    }

    public static void main(String[] args) {
        String stringWithComma = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22";
        List<String> strList = CollectionUtil.toList(stringWithComma.split(","));
        List<?> pageRows = rowsToPageRows(strList, 2, 5);
        assert pageRows != null;
        pageRows.forEach(System.out::println);
    }

}
